package com.team20.map_demo.controller;

import com.team20.map_demo.domain.Approval;
import com.team20.map_demo.domain.SearchApprovalResponse;
import com.team20.map_demo.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

@Controller
@RequestMapping("/appr")
public class ApprovalController {
    @Autowired
    private ApprovalService approvalService;

//    @RequestMapping("/toadd")
//    public String toAdd(){
//        return "appr_add";
//    }

    @RequestMapping("/add")
    public String add(String apprState, Short apprRank, MultipartFile file) throws IOException {
        if (file != null) {
            Approval approval = new Approval();

            approval.setApprovalState(apprState);
            approval.setApprovalRank(apprRank);
            approval.setApprovalDate(new Date());

            approval.setFileName(file.getOriginalFilename());
            approval.setFileType(file.getContentType());
            approval.setFileLength(new BigDecimal(file.getSize()));
            approval.setFile(file.getBytes());

            approvalService.saveAppr(approval);
            return "redirect:/appr/search";
        }
//        approvalService.insert(approval);
        return "appr_add";
    }

    @RequestMapping("/download")
    public ResponseEntity<Resource> download(String id) {
        Approval approval = approvalService.selectByPrimaryKey(id);

        if(approval != null && approval.getFile() != null && approval.getFile().length > 0)
            return ResponseEntity.ok()
                    .contentType(MediaType.parseMediaType(approval.getFileType()))
                    .contentLength(approval.getFileLength().longValue())
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename = \"" + approval.getFileName() + "\"")
                    .body(new ByteArrayResource(approval.getFile()));
        else
            return (ResponseEntity<Resource>) ResponseEntity.notFound();
    }

    /*@RequestMapping("/search")
    public String apprPage(Model model, Integer pageNum, Integer pageSize) {
        List<Approval> approvals = approvalService.listAppr(pageNum, pageSize);
        model.addAttribute("approvals", approvals);
        return "appr_list";
    }*/

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String apprPage() {
        return "appr_list";
    }

    @RequestMapping(value = "/api/search", method = RequestMethod.GET)
    @ResponseBody
    public SearchApprovalResponse searchAppr(Integer pageNum, Integer pageSize) {
        return approvalService.listAppr(pageNum, pageSize);
    }

    @RequestMapping("/edit")
    public String editAppr(String id, Model model) {
        Approval approval = approvalService.selectByPrimaryKey(id);
        model.addAttribute("approval", approval);
        return "appr_add";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Boolean deleteAppr(String id){
        int deleted = approvalService.deleteById(id);
        return deleted > 0;
    }
}

